package com.dreamthen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;

/**
 * @Author zengms
 * @Date 2023-11-05 17:03:02
 **/
@Controller
@RequestMapping("/session")
public class SessionController {

    @Autowired
    private HttpSession httpSession;

    /**
     * 通过原生session方式传递参数给页面
     * @param session
     * @return
     */
    @GetMapping("/01")
    public String session01(HttpSession session){
        session.setAttribute("type","HttpSession");
        return "main";
    }

    /**
     * 通过自动注入方式，把参数传递给页面（推荐用这种方式）
     * @return
     */
    @GetMapping("/02")
    public String session02(){
        httpSession.setAttribute("type","HttpSession-Autowired");
        return "main";
    }


    /**
     * 通过SessionAttribute，获取session参数
     * @return
     */
    @GetMapping("/03")
    public String session03(@SessionAttribute(name = "type",required = false) String type){
        System.out.println("****sessionType-03="+type);
        return "main";
    }
}
